package info.thereisonlywe.core.essentials;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.xml.sax.InputSource;

public class DataEssentials {

	public static <T> boolean contains(T[] a, Object o)
	{
		if (a == null) return false;

		for (int i = 0; i < a.length; i++)
		{
			if (a[i].equals(o)) return true;
		}
		return false;
	}

	// Enlarges array and inserts value into given position
	public static String[] insertAtBeginning(String[] array, String value)
	{
		String[] result = new String[array.length + 1];
		result[0] = value;
		for (int i = 1; i < result.length; i++)
		{
			result[i] = array[i - 1];
		}
		return result;
	}

	public static int[] toIntArray(String[] s)
	{
		int[] result = new int[s.length];
		for (int i = 0; i < s.length; i++)
		{
			result[i] = Integer.parseInt(s[i]);
		}
		return result;
	}

	public static Integer[] toIntegerArray(String[] s)
	{
		Integer[] result = new Integer[s.length];
		for (int i = 0; i < s.length; i++)
		{
			result[i] = Integer.parseInt(s[i]);
		}
		return result;
	}

	public static InputStream toInputStream(String str, String encoding)
	{
		byte[] bytes = null;
		try
		{
			bytes = str.getBytes(encoding);
		}
		catch (UnsupportedEncodingException ex)
		{
		}
		return new ByteArrayInputStream(bytes);
	}

	public static InputSource toInputSource(String str)
	{
		InputSource s = new InputSource(new StringReader(str));
		return s;
	}

	public static int[] toIntArray(Integer[] val)
	{
		int[] result = new int[val.length];
		System.arraycopy(val, 0, result, 0, result.length);
		return result;
	}

	public static int[] toIntArray(List<Integer> integers)
	{
		int[] ret = new int[integers.size()];
		for (int i = 0; i < ret.length; i++)
		{
			ret[i] = integers.get(i).intValue();
		}
		return ret;
	}

	public static List<String> toList(String[] s)
	{
		return Arrays.asList(s);
	}

	public static String[] toStringArray(int[] array)
	{
		String[] res = new String[array.length];
		for (int i = 0; i < array.length; i++)
		{
			res[i] = String.valueOf(array[i]);
		}
		return res;
	}

	public static <T> List<T> getIntersection(List<T> l1, List<T> l2)
	{
		l1.retainAll(l2);
		return l1;
	}

	public static <T> List<T> getNegationOfIntersection(List<T> l1, List<T> l2)
	{
		List<T> u = getUnion(l1, l2);
		l1.retainAll(l2);
		u.removeAll(l1);
		return u;
	}

	public static <T> List<T> getUnion(List<T> l1, List<T> l2)
	{
		l1.removeAll(l2);
		l1.addAll(l2);
		return l1;
	}

	public static <T> Set<T> getUnion(Set<T> a, Set<T> b)
	{
		Set<T> c = new TreeSet<T>(a);
		c.addAll(b);
		return c;
	}

	public static <T> Set<T> getIntersection(Set<T> a, Set<T> b)
	{
		Set<T> d = new TreeSet<T>(a);
		d.retainAll(b);
		return d;
	}

	public static <T> Set<T> getDifference(Set<T> a, Set<T> b)
	{
		Set<T> e = new TreeSet<T>(a);
		e.removeAll(b);
		return e;
	}

	public static <T> List<T> getReverse(Set<T> a)
	{
		List<T> list = new ArrayList<T>(a);
		java.util.Collections.reverse(list);
		return list;
	}

}
